package models

import (
	"VoteProgram/dao"
	"github.com/jinzhu/gorm"
)

//每位参赛学生信息的结构体
type Students struct {
	ID int32
	Name string
	QQ string
	Thumbs int32
	Img string
}

//记录访问量
type Visitors struct {
	Num int
}

//每天初始化点赞的数据库
func InitThumbData()  {
	yesterday,_ := dao.GetTime()
	dao.DB.Debug().Where("date=?",yesterday).Delete(&ThumbsIp{})
}

//记录点赞ip
type ThumbsIp struct {
	Ip string
	Thumbs int
	Date string
}
//访问量+1
func VisitorPlus1(){
	dao.DB.Model(&Visitors{}).UpdateColumn("num",gorm.Expr("num+?",1))
}
//获取访问量
func GetVisitors()(Visitor Visitors){
	dao.DB.Model(&Visitors{}).Find(&Visitor)
	return Visitor
}

//检验点赞是否满足条件
func IfThumbs(Cli_ip string,today string)(if_thumb bool){
	var thumb_ip ThumbsIp
	//查询数据库是否存在请求点赞的ip
	dao.DB.Model(&ThumbsIp{}).Where("ip=?",Cli_ip).Find(&thumb_ip)
	//如果不存在
	if thumb_ip == (ThumbsIp{}) {
		if_thumb = true
		dao.DB.Create(ThumbsIp{Ip: Cli_ip,Thumbs: 0,Date: today})
	}else if thumb_ip != (ThumbsIp{}) && thumb_ip.Thumbs <3{	//存在且点赞次数小于3
		if_thumb = true
	}else {		//不存在
		if_thumb = false
	}
	return
}
//点赞数+1
func ThumbsPlus1(id string)  {
	dao.DB.Model(&Students{}).Where("id=?",id).Update("thumbs",gorm.Expr("thumbs+?", 1))
}
//点赞ip的次数 +1
func ThumbsIpPlus1(Cli_ip string){
	dao.DB.Model(&ThumbsIp{}).Where("ip=?",Cli_ip).Update("thumbs",gorm.Expr("thumbs+?", 1))
}
//查询学生详细信息
func GetStudentInfo(id string)(Student_info Students){
	dao.DB.Where("id=?",id).Find(&Student_info)
	return
}
